﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Web.Areas.Admin.Models.Login
{
    public partial class RequestPasswordResetModel : IDataErrorInfo
    {
        public bool IsFormSuccess { get; set; }

        [Required(ErrorMessage = "Polje ne smije biti prazno!")]
        [StringLength(40, MinimumLength = 4, ErrorMessage = "Korisničko ime mora biti između 4 i 40 znakova duljine!")]
        public string Username { get; set; }

        //[Required(ErrorMessage = "Potrebno je riješiti captcha izazov.")]
        //public string recaptcha_response_field { get; set; }
        //TODO: dodat captchu
    }

    public partial class RequestPasswordResetModel
    {
        public IUser User
        {
            get
            {
                if (_user == null)
                {
                    _user = new Lazy<IUser>(() => _userQueries.TryGetWhereUsername(this.Username));
                }
                return _user.Value;
            }
        }
        private Lazy<IUser> _user;
    }

    public partial class RequestPasswordResetModel
    {
        public string this[string prop]
        {
            get
            {
                if (prop == "Username")
                {
                    if (this.Username != null)
                    {
                        if (_userQueries.TryGetWhereUsername(this.Username) == null)
                        {
                            return "Korisnik s tim korisničkim imenom ne postoji!";
                        }
                    }
                }

                //if (prop == "recaptcha_response_field" && this.recaptcha_response_field != null)
                //{
                //    if (!ReCaptcha.Validate(ConfigurationManager.AppSettings["ReCaptchaPrivateKey"]))
                //    {
                //        return "Vaše rješenje captcha izazova je pogrešno.";
                //    }
                //}

                return null;
            }
        }

        public string Error
        {
            get { return null; }
        }
    }

    public partial class RequestPasswordResetModel
    {
        private readonly IUserQueries _userQueries;

        public RequestPasswordResetModel(
            IUserQueries userQueries)
        {
            _userQueries = userQueries;
        }
    }
}